package com.hghivln.controller.system;

import com.hghivln.aspectj.annotation.Log;
import com.hghivln.aspectj.enums.BusinessType;
import com.hghivln.pojo.dto.DictTypeDto;
import com.hghivln.pojo.vo.AjaxResult;
import com.hghivln.service.DictTypeService;

import com.hghivln.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;

@RestController
@RequestMapping("system/dict/type")
public class DictTypeController {

    @Autowired
    private DictTypeService dictTypeService;

    /**
     * 分页查询
     */
    @GetMapping("listForPage")
    public AjaxResult listForPage(DictTypeDto dictTypeDto) {
        return dictTypeService.listPage(dictTypeDto);
    }

    /**
     * 查询所有可用字典类型
     */
    @GetMapping("selectAllDictType")
    public AjaxResult selectAllDictType() {
        return dictTypeService.list();
    }

    /**
     * 添加
     */
    @PostMapping("addDictType")
    @Log(title = "添加字典类型",businessType = BusinessType.INSERT)
    public AjaxResult addDictType(@Validated DictTypeDto dictTypeDto) {
        if (dictTypeService.checkDictTypeUnique(dictTypeDto.getDictId(),dictTypeDto.getDictType())) {
            return AjaxResult.fail("新增字典【"+dictTypeDto.getDictName()+"】失败，字典类型已存在");
        }
        dictTypeDto.setSimpleUser(ShiroUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(dictTypeService.insert(dictTypeDto));
    }

    /**
     * 修改
     */
    @PutMapping("updateDictType")
    @Log(title = "修改字典类型",businessType = BusinessType.UPDATE)
    public AjaxResult updateDictType(@Validated DictTypeDto dictTypeDto) {
        if (dictTypeService.checkDictTypeUnique(dictTypeDto.getDictId(),dictTypeDto.getDictType())) {
            return AjaxResult.fail("修改字典【"+dictTypeDto.getDictName()+"】失败，字典类型已存在");
        }
        dictTypeDto.setSimpleUser(ShiroUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(dictTypeService.update(dictTypeDto));
    }

    /**
     * 删除
     */
    @DeleteMapping("deleteDictTypeByIds/{dictIds}")
    @Log(title = "删除字典类型",businessType = BusinessType.DELETE)
    public AjaxResult deleteDictTypeByIds(@PathVariable @Validated @NotEmpty Long[] dictIds) {
        return AjaxResult.toAjax(dictTypeService.deleteDitypeByIds(dictIds));
    }

    /**
     * 根据ID查询一个字典信息
     */
    @GetMapping("getOne/{dictId}")
    public AjaxResult getDictTypeById(@PathVariable @Validated @NotEmpty(message = "字典ID不能为空") Long dictId) {
        return AjaxResult.success(dictTypeService.selectDictTypeById(dictId));
    }

    /**
     * 缓存同步
     */
    @GetMapping("dictCacheAsync")
    @Log(title = "缓存同步到redis")
    public AjaxResult dictCacheAsync() {
        try {
            dictTypeService.dictCacheAsync();
            return AjaxResult.success();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return AjaxResult.error();
        }
    }
}
